<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class NoticeController extends Controller
{
	
	//公告列表显示
	public function getGuanli(Request $request)
	{
		//提取所有数据
		if($request['name']){
			$notice = DB::table('notice')
			->leftJoin('proclamation','notice.ProclamationId','=','proclamation.id')
			->where('Title','like','%'.$request['name'].'%')
            ->paginate(10);
		}else{
			$notice = DB::table('notice')
			->leftJoin('proclamation','notice.ProclamationId','=','proclamation.id')
			->orderBy('Top','desc')
            ->paginate(3);
			}
			$info = $request -> all();
			// dd($notice);
		return view('Lzk.Notice.Notice',['notice'=>$notice,'info'=>$info]);

		
	}

	//解析添加页面
	public function getAdd()
	{
		$add = DB::table('Proclamation')->get();
		// dd($add);
		$bumen = DB::table('department')->get();
		// dd($bumen);
		return view('Lzk.Notice.AddNotice',['add'=>$add,'bumen'=>$bumen]);
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$name = $request->session()->get('username');
		// dd($name);
		$data = $request->only('Title','ProclamationId','DepartmentID','Notice','Top');
		$data['PerName'] = $name; 
		$time = time();
		$data['Date'] = $time;
		$res = DB::table('notice')->insert($data);
		if($res){
			return redirect('Notice/Notice/guanli');
		}else{
			return back();
		}
	}
	// 公告修改
	public function getEdit($id)
	{
		$edits = DB::table('notice')->where('NoticeID',$id)->first();
		$edit = DB::table('Proclamation')->get();
		return view('Lzk.Notice.EditNotice',['edits'=>$edits,'edit'=>$edit]);
	}
	//执行修改
	public function postUpdate(Request $request)
	{
        $res = $request->only('Title','ProclamationId','Notice');
        $id = $request->only('NoticeID');
        $time = time();
        $res['Date'] = $time;
        $res = DB::table('notice')->where('NoticeID',$id)->update($res);
        if($res){
        	return redirect('/Notice/Notice/guanli')->with('修改成功');
        }else{
        	return back();
        }
	} 
	//公告详情
	public function getLook($id)
	{
		$edits = DB::table('notice')
						->join('department','notice.DepartmentID','=','department.id')
						->where('NoticeID',$id)->first();
		// dd($edits);
		return view('Lzk.Notice.NoticeShowDetail',['edits'=>$edits]);
	}


	public function  getDel($id)
	{
		$del = DB::select("delete from notice where NoticeID = '{$id}'");
		if($del){
        	return redirect('/Notice/Notice/guanli');
        }else{
        	return back();
        }
	}



}
